<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\common\controller\ServerResponse;
use app\model\Blogs;
use app\model\Blogtype;
use app\model\Hot_update;
use app\model\Link;
use app\Request;

class Blog extends ServerResponse
{
    //热更新
    public function updateHotRequest(){
        $data = Hot_update::getHotUpdate();
        if($data){
            if($data['hot']==1){
                Hot_update::hotUpdate(0); //重置更新状态
                return ServerResponse::jsonData(1,'当前需热更新!',$data);
            }else{
                return ServerResponse::jsonData(1,'无需更新',$data);
            }
        }else{
            return ServerResponse::jsonData(0,'操作失败!',null);
        }
    }
    //文章访问量更新
    public function updateBlogVisited(Request $request){
        $bid = $request->post('bid');
        $data = Blogs::updateVisited($bid);
        if($data){
            return ServerResponse::jsonData(1,'更新访问量成功!',$data);
        }else{
            return ServerResponse::jsonData(0,'更新访问量失败!',null);
        }
    }
    //获取侧边栏数据
    public function getSideData(){
        $link = Link::getLink();//获取友情链接
        $hotBlog = Blogs::hotBlog();//获取热文前十
        $yearMonth= Blogs::getYearMonth();//获取按年月归档
        $category = Blogs::getCategoryList();//获取按分类归档
        if($link&&$hotBlog&&$yearMonth&&$category){
            return ServerResponse::jsonData(1,'获取侧边栏数据成功!',['link'=>$link,'hotBlog'=>$hotBlog,'yearMonth'=>$yearMonth,'category'=>$category]);
        }else{
            return ServerResponse::jsonData(1,'获取侧边栏数据失败!',null);
        }
    }
    //获取友情链接
    public function getLink(){
        $data = Link::getLink();
        if($data){
            return ServerResponse::jsonData(1,'获取友情链接成功!',$data);
        }else{
            return ServerResponse::jsonData(0,'获取友情链接失败!',null);
        }
    }
    //获取热文前十排行
    public function getHotBlog(){
        $data = Blogs::hotBlog();
        if($data){
            return ServerResponse::jsonData(1,'获取热文前十排行成功!',$data);
        }else{
            return ServerResponse::jsonData(0,'获取热文前十排行失败!',null);
        }
    }
    //展示抽屉导航 h5
    public function getDrawer(){
        $drawerList = Blogtype::where('sid',0)->select();
        if($drawerList){
            return ServerResponse::jsonData(1,'请求成功',$drawerList);
        }else{
            return ServerResponse::jsonData(0,'请求失败',null);
        }
    }
    //获取h5 抽屉下的所有博客列表
    public function getDrawerBlogList(){
        $blogList = Blogs::getDrawer();
        $navList = Blogtype::getNavList();
        if($blogList&&$navList){
            return ServerResponse::jsonData(1,'请求成功',['blogList'=>$blogList,'navList'=>$navList]);
        }else{
            return ServerResponse::jsonData(0,'请求失败',null);
        }
    }
    //获取博客数据列表
    public function getBlogList(Request $request){
        $tid=$request->get('tid')??'';
        $limit=$request->get('limit')??10;
        $page=$request->get('page')??1;
        if($tid==''){
            $data = Blogs::select();
        }else{
            $arrs = Blogtype::select();
            $allTids=[$tid];
            foreach ($arrs as $k => $v){
                if($tid == $v['sid']){
                    array_push($allTids,$v['tid']);
                }
            }
            $data = Blogs::whereOr(['tid'=>$allTids])->order('uploaddate','desc')->paginate(['list_rows'=>$limit,'page'=>$page]);
        }
        if($data){
            return ServerResponse::jsonData(1,'获取列表数据成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'获取列表数据失败！',$data);
        }
    }
    //获取博文详情
    public function blogDetail(Request $request){
        $bid = $request->get('bid')??1;
        $data = Blogs::where('bid',$bid)->find();
        if($data){
            return ServerResponse::jsonData(1,'获取文章详情成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'获取文章详情失败！',null);
        }
    }
  //博文搜索功能
    public function blogSearch(Request $request){
        $key = $request->post('key');
        $limit = $request->post('limit')??10;
        $page = $request->post('page')??1;
        $data=Blogs::getSearch($key,$limit,$page);
        if($data){
            $data = $data->toArray();
            foreach ($data['data'] as &$v){
                $v['url']=DOMAIN_NAME.$v['pic'];
            }
            return ServerResponse::jsonData(1,'请求成功!',$data);
        }else{
            return ServerResponse::jsonData(1,'请求失败!',null);
        }
    }
    //根据tid获取文章列表
    public function getTidBlogList(Request $request){
        $tid = $request->post('tid');
        $limit = $request->post('limit')??10;
        $page = $request->post('page')??1;
        $data = Blogs::getTidBlogList($tid,$limit,$page);
        if($data){
            $data = $data->toArray();
            foreach ($data['data'] as &$v){
                $v['url']=DOMAIN_NAME.$v['pic'];
            }
            return ServerResponse::jsonData(1,'请求成功',$data);
        }else{
            return ServerResponse::jsonData(0,'请求失败',null);
        }
    }
    //根据年月获取文章列表
    public function getYearDayBlogList(Request $request){
        $date = $request->post('date');
        $limit = $request->post('limit')??10;
        $page = $request->post('page')??1;
        $data = Blogs::getYearDayBlogList($date,$limit,$page);
        if($data){
            foreach ($data['data'] as &$v){
                $v['url']=DOMAIN_NAME.$v['pic'];
            }
            return ServerResponse::jsonData(1,'获取年月归档文章列表成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'获取年月归档文章列表失败！',null);
        }
    }
    //获取文章详情
    public function getBlogDetail(Request $request){
        $bid = $request->post('bid')??1;
        $data = Blogs::where('bid',$bid)->find();
        $previous = Blogs::getBlogNext($bid,'<','desc');//获取上一篇
        $next = Blogs::getBlogNext($bid,'>','asc');//获取下一篇
        if($data){
            $data['url']=DOMAIN_NAME.$data['pic'];
            $data['detail']=html_entity_decode($data['detail']);
            $data['previousBlog'] = $previous;
            $data['nextBlog']=$next;
            return ServerResponse::jsonData(1,'获取文章详情成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'获取文章详情失败！',null);
        }
    }
}
